home *** CD-ROM | disk | FTP | other *** search
/ 9-Digit Zip Code Directory / 9-Digit Zip Code Directory (American Business Information) (ABIZIP-12).ISO / z4src.zip / Z4CXCP.C < prev    next >
C/C++ Source or Header  |  1995-05-21  |  7KB  |  215 lines

  1. //----------------------------------------------------------------------------
  2. //                            MODULE DESCRIPTION
  3. //
  4. //  Module:    z4cxcp.c
  5. //   Title:    ZIP+4 Engine
  6. //  Notice:    John M. Weeder
  7. //                 Copyright (c) 1993. All rights reserved.
  8. //             This module contains proprietary information and should be 
  9. //                treated as confidential.
  10. //
  11. //----------------------------------------------------------------------------
  12. //                           MAINTENANCE HISTORY
  13. //
  14. // $Workfile$
  15. // $Revision$
  16. //   $Author$
  17. //     $Date$
  18. //      $Log$    
  19. //
  20. //----------------------------------------------------------------------------
  21. //                             MODULE NARRATIVE
  22. //
  23. //
  24. //    This module contains the compressor for the POF file.
  25. //
  26. //    The code in this module should be written entirely in C. 
  27. //    Do not use any C++ constructs.
  28. //
  29. //    This module is portable to:
  30. //        DOS 3.X+
  31. //        MS Windows 3.X+
  32. //        OS/2 2.X+
  33. //        OS/2 2.0 PM
  34. //        SCO UNIX.
  35. //
  36. //    The following compilers are supported:
  37. //        MSC 6.0A
  38. //        MSC/C++ 7.0
  39. //        Borland C++ 3.1 for DOS
  40. //        Borland C++ 1.0 for OS/2 2.X
  41. //        SCO UNIX cc
  42. //
  43. //----------------------------------------------------------------------------
  44. #include <z4.h>
  45.  
  46.  
  47. //----------------------------------------------------------------------------
  48. //    Prototypes
  49. //----------------------------------------------------------------------------
  50. static BOOL FN_L Z4CXCompressAppend(PDATACOMP, PZ4_CX);
  51.  
  52.  
  53. //----------------------------------------------------------------------------
  54. //   Description:    Compression function    
  55. //    Parameters:    pdatacomp    Compressor data
  56. //       Returns:    Compression function result code. See data.h.
  57. //----------------------------------------------------------------------------
  58. SHORT FN_E Z4CXCompress(PDATACOMP pdatacomp)
  59. {
  60. static BOOL fPending;            // ZIP5 record pending?
  61. static Z4_CX cx;            // ZIP5 record
  62. static SIZET cMaxZips;
  63. static SIZET cMaxPofs;
  64. static SIZET cPofCities;
  65. static CHAR szZip5[MAX_ZIP5+1];
  66. static SIZET cZip;
  67. static SIZET cState;
  68. static SIZET cCity;
  69. static SIZET cLLName;
  70. static SIZET cFacility;
  71. static SIZET cPof;
  72.             
  73.     LONG lRec = pdatacomp->dlmrec.lId;
  74.     PPSZ ppsz = pdatacomp->dlmrec.apsz;
  75.     SIZET i;
  76.     Z4_STATE state;
  77.     CHAR szCity[MAX_CITY+1];
  78.  
  79.     switch (lRec)
  80.         {
  81.         case DAI_INITIALIZE:                    // Initialize at startup
  82.             cMaxZips = 0;
  83.             cMaxPofs = 0;
  84.             cPofCities = 0;
  85.             Assert(MAX_CITY_ZIP5 <= pdatacomp->cbMax / MAX_ZIP5_BCD);
  86.             cZip           = DataField(pdatacomp->dlmrec.pcfg, "zipcode");
  87.             cPof           = DataField(pdatacomp->dlmrec.pcfg, "finance");
  88.             cState         = DataField(pdatacomp->dlmrec.pcfg, "state_abbrev");
  89.             cCity       = DataField(pdatacomp->dlmrec.pcfg, "city_name");
  90.             cLLName    = DataField(pdatacomp->dlmrec.pcfg, "last_line_name");
  91.             cFacility   = DataField(pdatacomp->dlmrec.pcfg, "facility");
  92.         case DAI_START_BLK:                    // Start block
  93.             fPending = FALSE;
  94.             return DAO_SUCCESS;
  95.  
  96.         case DAI_TERMINATE:                    // Terminate
  97.             Output("\nMaximum of %u ZIPs in a city/state.\n", cMaxZips);
  98.             Output("Maximum of %u POFs in a city/state.\n", cMaxPofs);
  99.             Output("%u city/states with > 1 POF.\n", cPofCities);
  100.         case DAI_FAILURE:
  101.         case DAI_END_BLK:                        // End block
  102.             return DAO_SUCCESS;
  103.  
  104.         case DAI_LAST_REC:
  105.             if (!fPending)
  106.                 return DAO_FAILURE;
  107.             if (cx.cFinance > 1)
  108.                 cPofCities++;
  109.             Z4CXCompressAppend(pdatacomp, &cx);
  110.             return DAO_FLUSH;
  111.         }
  112.     if (pdatacomp->dlmrec.fSkipped)
  113.         return DAO_SKIP;
  114.                                                     // Get state code
  115.     state = Z4FindState(ppsz[cState]);
  116.     Assert(state != Z4_ST_INVALID);
  117.     strcpy(szCity, Z4Clean(ppsz[cCity]));
  118.  
  119.     if (fPending)
  120.         {
  121.         if (state != cx.state
  122.         || strcmp(cx.szCity, szCity) != 0)
  123.             {
  124.             if (cx.cFinance > 1)
  125.                 cPofCities++;
  126.             Z4CXCompressAppend(pdatacomp, &cx);
  127.             fPending = FALSE;
  128.             return DAO_MARK_FLUSH;
  129.             }
  130.         if (strcmp(szZip5, ppsz[cZip]) != 0)
  131.             {
  132.             Assert(cx.cZip5 < MAX_CITY_ZIP5);
  133.             strcpy(szZip5, ppsz[cZip]);
  134.             stra2b(cx.abZip5[cx.cZip5], MAX_ZIP5_BCD, ppsz[cZip], MAX_ZIP5);
  135.             cx.cZip5++;
  136.             cMaxZips = MAX(cMaxZips, cx.cZip5);
  137.             }
  138.         for (i = 0; i < cx.cFinance; ++i)
  139.             {
  140.             CHAR szFinance[MAX_FINANCE+1];
  141.  
  142.             strb2a(cx.abFinance[i], MAX_FINANCE_BCD, szFinance, MAX_FINANCE, TRUE);
  143.             szFinance[MAX_FINANCE] = '\0';
  144.             if (strcmp(szFinance, ppsz[cPof]) == 0)
  145.                 break;
  146.             }
  147.         if (i >= cx.cFinance)
  148.             {
  149.             Assert(cx.cFinance < MAX_POF_CITY);
  150.             stra2b(cx.abFinance[cx.cFinance], MAX_FINANCE_BCD, ppsz[cPof], MAX_FINANCE);
  151.             cx.cFinance++;
  152.             cMaxPofs = MAX(cMaxPofs, cx.cFinance);
  153.             }
  154.         return DAO_NEXT;
  155.         }
  156.     fPending = TRUE;
  157.     memset(&cx, 0, sizeof(cx));
  158.     cx.state = state;
  159.     strcpy(cx.szCity, szCity);
  160.     if (ppsz[cFacility][0] == '9')
  161.         strcpy(cx.szPO, Z4Clean(ppsz[cLLName]));
  162.     stra2b(cx.abFinance[cx.cFinance], MAX_FINANCE_BCD, ppsz[cPof], MAX_FINANCE);
  163.     cx.cFinance++;
  164.     strcpy(szZip5, ppsz[cZip]);
  165.     stra2b(cx.abZip5[cx.cZip5], MAX_ZIP5_BCD, ppsz[cZip], MAX_ZIP5);
  166.     cx.cZip5++;
  167.     cMaxZips = MAX(cMaxZips, cx.cZip5);
  168.     cMaxPofs = MAX(cMaxPofs, cx.cFinance);
  169.     return DAO_NEXT;
  170. }
  171.  
  172.  
  173. //----------------------------------------------------------------------------
  174. //   Description:    Compress the current ZIP5 record
  175. //    Parameters:    pdatacomp    Compresser data
  176. //                        pcx            POF record
  177. //       Returns:    TRUE if successful.
  178. //----------------------------------------------------------------------------
  179. static BOOL FN_L Z4CXCompressAppend(PDATACOMP pdatacomp, PZ4_CX pcx)
  180. {
  181.     PBYTE pb = pdatacomp->pb;
  182.     SIZET cb, cLen;
  183.  
  184.     *pb = (BYTE)pcx->state;                    // Store state
  185.     pb++;
  186.     
  187.     cLen = strlen(pcx->szCity);            // Store city name
  188.     memcpy(pb, pcx->szCity, cLen + 1);
  189.     pb += cLen + 1;
  190.  
  191.     cLen = strlen(pcx->szPO);                // Store post office name
  192.     memcpy(pb, pcx->szPO, cLen + 1);
  193.     pb += cLen + 1;
  194.                                                     // Store POF count
  195.     *(PUSHORT)pb = (USHORT)pcx->cFinance;    
  196.     pb += sizeof(USHORT);
  197.                                                     // Store POFs
  198.     cb = MAX_FINANCE_BCD * pcx->cFinance;    
  199.     memcpy(pb, pcx->abFinance, cb);
  200.     pb += cb;
  201.  
  202.     *(PUSHORT)pb = (USHORT)pcx->cZip5;    // Store ZIP count
  203.     pb += sizeof(USHORT);
  204.  
  205.     cb = MAX_ZIP5_BCD * pcx->cZip5;        // Store ZIPs
  206.     memcpy(pb, pcx->abZip5, cb);
  207.     pb += cb;
  208.                                                     // Update number of bytes written
  209.     pdatacomp->cb = (SIZET)(pb - pdatacomp->pb);
  210.     return TRUE;
  211. }
  212. //----------------------------------------------------------------------------
  213. //------------------------------- End of File --------------------------------
  214. //----------------------------------------------------------------------------
  215.